from flask import Flask, request
from application02.utils import LogUtil as lp_logging
import application02.conf.config as config


def create_app(config_name):
    app = Flask(__name__)

    @app.before_request
    def before_request():
        # 在每次请求前生成新的 Trace ID
        lp_logging.TraceID.set_trace(lp_logging.generate_trace_id())
        lane_code = request.headers.get('uberctx-rootlanecode')
        lp_logging.LaneCode.set_lane_code(lane_code if lane_code else 'trunk')

    # 读入一组配置项，并将他们一次性加载到Flask应用对象app的配置中
    # app.config.from_object 会将所选配置类的全部大写属性批量导入 Flask 配置。
    app.config.from_object(config.env_config[config_name])
    config.env_config[config_name].init_app(app)

    # 引入
    from application02.api.workorder import workorder as workorder_blueprint
    # 注册
    app.register_blueprint(workorder_blueprint)

    return app
